<?php
/**
 * Master Fetch Script
 * Runs all data fetchers in sequence
 * Location: /app/fetch/fetch_all.php
 */

require_once __DIR__ . '/../core/config.php';
require_once __DIR__ . '/../core/db.php';

echo "╔════════════════════════════════════════╗\n";
echo "║   ClientRadar - Master Data Fetch     ║\n";
echo "╚════════════════════════════════════════╝\n\n";

$start_time = time();
$results = [];

// 1. RemoteOK
if (file_exists(__DIR__ . '/remoteok_fetch.php')) {
    echo "1️⃣  Running RemoteOK fetcher...\n";
    echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n";
    ob_start();
    require __DIR__ . '/remoteok_fetch.php';
    if (function_exists('fetchRemoteOKJobs')) {
        $results['remoteok'] = fetchRemoteOKJobs();
    }
    ob_end_clean();
    echo "\n";
}

// 2. Freelancer.com
if (file_exists(__DIR__ . '/freelancer_fetch.php')) {
    echo "2️⃣  Running Freelancer.com fetcher...\n";
    echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n";
    ob_start();
    require_once __DIR__ . '/freelancer_fetch.php';
    if (function_exists('fetchFreelancerJobs')) {
        $results['freelancer'] = fetchFreelancerJobs();
    }
    ob_end_clean();
    echo "\n";
}

// 3. We Work Remotely
if (file_exists(__DIR__ . '/fetch_wwr.php')) {
    echo "3️⃣  Running We Work Remotely fetcher...\n";
    echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n";
    require_once __DIR__ . '/fetch_wwr.php';
    if (function_exists('fetchWWRJobs')) {
        $results['wwr'] = fetchWWRJobs();
    }
    echo "\n";
}

// 4. Jobicy
if (file_exists(__DIR__ . '/fetch_jobicy.php')) {
    echo "4️⃣  Running Jobicy fetcher...\n";
    echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n";
    require_once __DIR__ . '/fetch_jobicy.php';
    if (function_exists('fetchJobicyJobs')) {
        $results['jobicy'] = fetchJobicyJobs();
    }
    echo "\n";
}

// Calculate totals
$total_new = 0;
$total_duplicates = 0;
$total_errors = 0;

foreach ($results as $source => $result) {
    if (isset($result['new'])) $total_new += $result['new'];
    if (isset($result['duplicates'])) $total_duplicates += $result['duplicates'];
    if (isset($result['errors'])) $total_errors += count($result['errors']);
}

$duration = time() - $start_time;

// Final Summary
echo "\n╔════════════════════════════════════════╗\n";
echo "║         MASTER FETCH SUMMARY          ║\n";
echo "╚════════════════════════════════════════╝\n\n";

echo "📊 Overall Statistics:\n";
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n";
echo "✅ Total new projects: " . $total_new . "\n";
echo "⏭️  Total duplicates: " . $total_duplicates . "\n";
echo "❌ Total errors: " . $total_errors . "\n";
echo "⏱️  Duration: " . $duration . " seconds\n\n";

echo "📋 By Source:\n";
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n";
foreach ($results as $source => $result) {
    $new = $result['new'] ?? 0;
    $dupes = $result['duplicates'] ?? 0;
    $errors = isset($result['errors']) ? count($result['errors']) : 0;
    
    echo sprintf("%-20s: +%d new, %d dupes, %d errors\n", ucfirst($source), $new, $dupes, $errors);
}

// Get current database stats
try {
    $db = getDbConnection();
    
    echo "\n📊 Database Statistics:\n";
    echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n";
    
    // Total active projects
    $stmt = $db->query("SELECT COUNT(*) as total FROM projects WHERE status = 'active'");
    $total = $stmt->fetch(PDO::FETCH_ASSOC)['total'];
    echo "Total active projects: " . $total . "\n";
    
    // By source
    $stmt = $db->query("SELECT source, COUNT(*) as count FROM projects WHERE status = 'active' GROUP BY source ORDER BY count DESC");
    $sources = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    echo "\nProjects by source:\n";
    foreach ($sources as $source) {
        echo "  " . $source['source'] . ": " . $source['count'] . "\n";
    }
    
    // By category
    $stmt = $db->query("SELECT category, COUNT(*) as count FROM projects WHERE status = 'active' GROUP BY category ORDER BY count DESC");
    $categories = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    echo "\nProjects by category:\n";
    foreach ($categories as $category) {
        echo "  " . $category['category'] . ": " . $category['count'] . "\n";
    }
    
} catch (Exception $e) {
    echo "❌ Error getting database stats: " . $e->getMessage() . "\n";
}

echo "\n✅ Master fetch complete!\n";
echo "🕐 Completed at: " . date('Y-m-d H:i:s') . "\n\n";

// Return results if run programmatically
return $results;
?>